package com.cn.lp.export.server.entity;

import com.cn.lp.export.server.entity.convert.ExportStatusConvert;
import com.cn.lp.export.server.entity.convert.LinkedHashMapConvert;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 导出任务
 *
 * @author wuqirong
 * @date 2020-10-16
 * <p>
 * All rights Reserved, Designed www.xiao100.com
 */
@Entity
@Table(name = "export_task")
@EntityListeners(AuditingEntityListener.class)
public class ExportTask {

    /**
     * 唯一识别id
     */
    @Id
    @Column(nullable = false, unique = true)
    private long id;

    /**
     * 导出字段, 存在嵌套表头Map
     */
    @Column
    @Lob
    @Convert(converter = LinkedHashMapConvert.class)
    private LinkedHashMap<String, Object> exportFieldMap = new LinkedHashMap<>();

    /**
     * 导出状态
     */
    @Column
    @Convert(converter = ExportStatusConvert.class)
    private ExportStatus exportStatus;

    /**
     * Excel文件名(不带扩展名)
     */
    @Column
    private String excelName;

    /**
     * 总条数
     */
    @Column
    private long totalNum;

    /**
     * 当前条数
     */
    @Column
    private long currentNum;

    /**
     * 乐观锁代替分布式锁
     */
    @Version
    @Column
    private Integer version;

    /**
     * 错误信息，待定
     */
    @Column(length = 600)
    private String errorMsg;

    @Column
    @CreatedDate
    private Date createTime;

    @Column(length = 600)
    private String resultUrl;

    public static ExportTask of(long id, Map<String, Object> exportFieldMap, String excelName, long totalNum, String resultUrl) {
        ExportTask task = new ExportTask();
        task.id = id;
        task.exportFieldMap.putAll(exportFieldMap);
        task.excelName = excelName;
        task.totalNum = totalNum;
        task.currentNum = 0;
        task.exportStatus = ExportStatus.READY;
        task.resultUrl = resultUrl;
        return task;
    }

    public long getTotalNum() {
        return totalNum;
    }

    public long getCurrentNum() {
        return currentNum;
    }

    public Integer getVersion() {
        return version;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public String getResultUrl() {
        return resultUrl;
    }

    public void updateStatus(ExportStatus status) {
        this.exportStatus = status;
    }

    public void updateDataNum(long dataNum) {
        this.currentNum = this.currentNum + dataNum;
    }

    public boolean isFullData() {
        return this.totalNum <= this.currentNum;
    }

    public long getId() {
        return id;
    }

    public String getExcelName() {
        return excelName;
    }

    public Map<String, Object> getExportFieldMap() {
        return exportFieldMap;
    }

    public void export() {
        this.exportStatus = ExportStatus.RUNNING;
    }

    public void finish() {
        this.exportStatus = ExportStatus.FINISH;
    }

    public void fail(String msg) {
        this.exportStatus = ExportStatus.FAIL;
        this.errorMsg = msg;
    }

    public ExportStatus getExportStatus() {
        return this.exportStatus;
    }
}
